﻿using ERP_SaaS_WinForm.生产计划_派单_车间.ProductPlan;
using ERP_SaaS_WinForm.生产计划_派单_车间.BOM;
using ERP_SaaS_WinForm.生产计划_派单_车间.MaterialDetail;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin.SkinControl;

namespace ERP_SaaS_WinForm.生产计划_派单_车间.ProductPlanDetail
{
    public partial class ProductDetailMain : Form
    {
        private PlanData currentPlanData;
        private List<Product> productList = new List<Product>();
        private List<MaterialDetailData> materialList = new List<MaterialDetailData>();
        private CCWin.SkinControl.SkinDataGridView dgvMaterial;
        
        // BOM页面控件
        private CCWin.SkinControl.SkinComboBox cboProduct;
        private Panel pnlMaterials;
        private List<BOMData> bomDataList = new List<BOMData>();
        
        public ProductDetailMain(PlanData planData)
        {
            InitializeComponent();
            currentPlanData = planData;
            
            LoadPlanData();
        }
        private void LoadPlanData()
        {
            try
            {
                // 设置窗体标题
                this.Text = $"生产计划详情 - {currentPlanData.planName}";
                
                // 填充四个输入框的数据
                if (!string.IsNullOrEmpty(currentPlanData.planName))
                {
                    txtDetailName.Text = currentPlanData.planName;
                }
                
                if (!string.IsNullOrEmpty(currentPlanData.planCode))
                {
                    txtDetailCode.Text = currentPlanData.planCode;
                }
                
                if (!string.IsNullOrEmpty(currentPlanData.createName))
                {
                    txtCreateName.Text = currentPlanData.createName;
                }
                
                if (currentPlanData.createDate.HasValue)
                {
                    dtpCreateTime.text = currentPlanData.createDate.Value.ToString("yyyy-MM-dd");
                }
                else
                {
                    dtpCreateTime.text = DateTime.Now.ToString("yyyy-MM-dd");
                }
                
                // 设置窗体基本属性
                this.StartPosition = FormStartPosition.CenterParent;
                this.FormBorderStyle = FormBorderStyle.Sizable;
                this.MaximizeBox = true;
                this.MinimizeBox = true;
                
                // 设置窗体大小
                this.Size = new Size(1200, 800);
                
                // 设置TabPage的文本
                tabBasic.Text = "基础信息";
                tabBOM.Text = "BOM组成";
                tabProduct.Text = "生产产品";
                
                // 设置默认选中基础信息页面
                tabDetail.SelectedIndex = 0;
                
                // 可以在这里添加更多详细信息到TabPage中
                LoadBasicInfo();
                
                // 延迟初始化BOM页面，确保控件已完全加载
                this.Load += (sender, e) => {
                    try
                    {
                        // 初始化BOM页面
                        InitializeBOMPage();
                        
                        // 加载BOM数据
                        LoadBOMData();
                        
                        // 初始化生产产品页面
                        InitializeProductPage();
                        
                        // 加载产品数据
                        LoadProductData();
                        
                        // 初始化物料明细页面
                        InitializeMaterialPage();
                        
                        // 加载物料明细数据
                        LoadMaterialData();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"初始化页面时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载计划数据时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 加载基础信息到TabPage
        /// </summary>
        private void LoadBasicInfo()
        {
            try
            {
                // 计划编号
                if (!string.IsNullOrEmpty(currentPlanData.planCode))
                {
                    txtPlanCode.Text = currentPlanData.planCode;
                }
                
                // 计划名称
                if (!string.IsNullOrEmpty(currentPlanData.planName))
                {
                    skinTextBox1.Text = currentPlanData.planName;
                }
                
                // 来源类型
                if (currentPlanData.resource > 0)
                {
                    string resourceText = GetResourceText(currentPlanData.resource);
                    txtResource.Text = resourceText;
                }
                
                // 订单编号
                if (currentPlanData.salesOrderId.HasValue && currentPlanData.salesOrderId.Value > 0)
                {
                    txtOrderCode.Text = currentPlanData.salesOrderId.Value.ToString();
                }
                
                // 成品名称
                if (!string.IsNullOrEmpty(currentPlanData.productName))
                {
                    txtProductName.Text = currentPlanData.productName;
                }
                
                // 成品编号
                if (!string.IsNullOrEmpty(currentPlanData.productCode))
                {
                    txtProductCode.Text = currentPlanData.productCode;
                }
                
                // 规格型号
                if (!string.IsNullOrEmpty(currentPlanData.specification))
                {
                    txtSpecification.Text = currentPlanData.specification;
                }
                
                // 计划数量
                if (currentPlanData.planNum > 0)
                {
                    txtplannum.Text = currentPlanData.planNum.ToString();
                }
                
                // 单位
                if (!string.IsNullOrEmpty(currentPlanData.unitName))
                {
                    txtunit.Text = currentPlanData.unitName;
                }
                
                // 计划开工时间
                if (currentPlanData.planStart.HasValue)
                {
                    dtpstarttime.text = currentPlanData.planStart.Value.ToString("yyyy-MM-dd");
                }
                
                // 计划完工时间
                if (currentPlanData.planEnd.HasValue)
                {
                    dtpcompletetime.text = currentPlanData.planEnd.Value.ToString("yyyy-MM-dd");
                }
                
                // 需求日期
                if (currentPlanData.requireDate.HasValue)
                {
                    dtprequiretime.text = currentPlanData.requireDate.Value.ToString("yyyy-MM-dd");
                }
             
            }
            catch (Exception ex)
            {
                Console.WriteLine($"加载基础信息时出错: {ex.Message}");
                MessageBox.Show($"加载基础信息时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 获取来源类型的中文描述
        /// </summary>
        /// <param name="resourceValue">来源类型值</param>
        /// <returns>中文描述</returns>
        private string GetResourceText(int resourceValue)
        {
            switch (resourceValue)
            {
                case 1:
                    return "销售订单";
                case 2:
                    return "库存备货";
                case 3:
                    return "其他";
                default:
                    return "未知";
            }
        }
        
        /// <summary>
        /// 初始化BOM页面 - 使用下拉框形式显示产品及其物料
        /// </summary>
        private void InitializeBOMPage()
        {
            try
            {
                // 清除原有的DataGridView
                dgvBOM.Visible = false;
                
                // 创建产品选择下拉框
                var lblProduct = new CCWin.SkinControl.SkinLabel();
                lblProduct.Text = "选择产品：";
                lblProduct.Location = new Point(20, 20);
                lblProduct.Size = new Size(80, 25);
                lblProduct.Font = new Font("微软雅黑", 9F);
                tabBOM.Controls.Add(lblProduct);
                
                var cboProduct = new CCWin.SkinControl.SkinComboBox();
                cboProduct.Name = "cboProduct";
                cboProduct.Location = new Point(110, 18);
                cboProduct.Size = new Size(200, 25);
                cboProduct.Font = new Font("微软雅黑", 9F);
                cboProduct.DropDownStyle = ComboBoxStyle.DropDownList;
                cboProduct.SelectedIndexChanged += CboProduct_SelectedIndexChanged;
                tabBOM.Controls.Add(cboProduct);
                
                // 创建物料显示区域
                var lblMaterials = new CCWin.SkinControl.SkinLabel();
                lblMaterials.Text = "物料组成：";
                lblMaterials.Location = new Point(20, 60);
                lblMaterials.Size = new Size(80, 25);
                lblMaterials.Font = new Font("微软雅黑", 9F, FontStyle.Bold);
                tabBOM.Controls.Add(lblMaterials);
                
                // 创建物料列表显示区域
                var pnlMaterials = new Panel();
                pnlMaterials.Name = "pnlMaterials";
                pnlMaterials.Location = new Point(20, 90);
                pnlMaterials.Size = new Size(tabBOM.Width - 40, tabBOM.Height - 120);
                pnlMaterials.AutoScroll = true;
                pnlMaterials.BorderStyle = BorderStyle.FixedSingle;
                pnlMaterials.BackColor = Color.White;
                tabBOM.Controls.Add(pnlMaterials);
                
                // 保存控件引用
                this.cboProduct = cboProduct;
                this.pnlMaterials = pnlMaterials;
                
                Console.WriteLine("BOM页面初始化完成，已创建下拉框和物料显示区域");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"初始化BOM页面时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 初始化生产产品页面
        /// </summary>
        private void InitializeProductPage()
        {
           
               
                // 设置生产产品 DataGridView的基本属性
                dgvProducts.AutoGenerateColumns = false;
                
                // 设置CSkin DataGridView样式
                dgvProducts.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(240, 248, 255);
                dgvProducts.DefaultCellStyle.SelectionBackColor = Color.FromArgb(59, 188, 240);
                dgvProducts.DefaultCellStyle.SelectionForeColor = Color.White;
                dgvProducts.RowHeadersVisible = false;
                dgvProducts.AllowUserToAddRows = false;
                dgvProducts.AllowUserToDeleteRows = false;
                dgvProducts.ReadOnly = true;
                dgvProducts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvProducts.MultiSelect = false;
                
                // 设置CSkin特有的样式
                dgvProducts.BackColor = Color.White;
                dgvProducts.BorderStyle = BorderStyle.None;
                dgvProducts.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                dgvProducts.RowTemplate.Height = 30;
                
                Console.WriteLine("生产产品页面初始化完成");
           
        }
        
        /// <summary>
        /// 初始化物料明细页面
        /// </summary>
        private void InitializeMaterialPage()
        {
            try
            {
                // 创建物料明细TabPage
                var materialTabPage = new CCWin.SkinControl.SkinTabPage();
                materialTabPage.Text = "物料明细";
                materialTabPage.Name = "materialTabPage";
                
                // 创建物料明细DataGridView控件
                var dgvMaterial = new CCWin.SkinControl.SkinDataGridView();
                dgvMaterial.Name = "dgvMaterial";
                dgvMaterial.Dock = DockStyle.Fill;
                dgvMaterial.Location = new Point(0, 0);
                dgvMaterial.Size = new Size(materialTabPage.Width, materialTabPage.Height);
                
                // 设置物料明细 DataGridView的基本属性
                dgvMaterial.AutoGenerateColumns = false;
                dgvMaterial.Columns.Clear();
                
                // 添加物料明细列定义
                // 物料编号列
                DataGridViewTextBoxColumn colMaterialCode = new DataGridViewTextBoxColumn();
                colMaterialCode.HeaderText = "物料编号";
                colMaterialCode.DataPropertyName = "materialCode";
                colMaterialCode.Width = 120;
                colMaterialCode.ReadOnly = true;
                dgvMaterial.Columns.Add(colMaterialCode);
                
                // 物料名称列
                DataGridViewTextBoxColumn colMaterialName = new DataGridViewTextBoxColumn();
                colMaterialName.HeaderText = "物料名称";
                colMaterialName.DataPropertyName = "materialName";
                colMaterialName.Width = 150;
                colMaterialName.ReadOnly = true;
                dgvMaterial.Columns.Add(colMaterialName);
                
                // 单位列
                DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
                colUnit.HeaderText = "单位";
                colUnit.DataPropertyName = "unit";
                colUnit.Width = 80;
                colUnit.ReadOnly = true;
                dgvMaterial.Columns.Add(colUnit);
                
                // 规格型号列
                DataGridViewTextBoxColumn colSpecification = new DataGridViewTextBoxColumn();
                colSpecification.HeaderText = "规格型号";
                colSpecification.DataPropertyName = "specification";
                colSpecification.Width = 120;
                colSpecification.ReadOnly = true;
                dgvMaterial.Columns.Add(colSpecification);
                
                // 物料属性列
                DataGridViewCheckBoxColumn colAttribute = new DataGridViewCheckBoxColumn();
                colAttribute.HeaderText = "物料属性";
                colAttribute.DataPropertyName = "attribute";
                colAttribute.Width = 100;
                colAttribute.ReadOnly = true;
                dgvMaterial.Columns.Add(colAttribute);
                
                // 设置CSkin DataGridView样式
                dgvMaterial.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(240, 248, 255);
                dgvMaterial.DefaultCellStyle.SelectionBackColor = Color.FromArgb(59, 188, 240);
                dgvMaterial.DefaultCellStyle.SelectionForeColor = Color.White;
                dgvMaterial.RowHeadersVisible = false;
                dgvMaterial.AllowUserToAddRows = false;
                dgvMaterial.AllowUserToDeleteRows = false;
                dgvMaterial.ReadOnly = true;
                dgvMaterial.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvMaterial.MultiSelect = false;
                
                // 设置CSkin特有的样式
                dgvMaterial.BackColor = Color.White;
                dgvMaterial.BorderStyle = BorderStyle.None;
                dgvMaterial.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                dgvMaterial.RowTemplate.Height = 30;
                
                // 将DataGridView添加到物料明细页面
                materialTabPage.Controls.Add(dgvMaterial);
                
                // 将物料明细页面添加到TabControl
                tabDetail.TabPages.Add(materialTabPage);
                
                // 保存控件引用
                this.dgvMaterial = dgvMaterial;
                
                Console.WriteLine("物料明细页面初始化完成，已添加5列");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"初始化物料明细页面时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        
     
      
        
        /// <summary>
        /// 加载BOM数据 - 按产品分组显示
        /// </summary>
        private async void LoadBOMData()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    
                    var response = await client.GetAsync("http://localhost:5281/api/Plan/GetBOM");
                    response.EnsureSuccessStatusCode();
                    
                    string jsonResponse = await response.Content.ReadAsStringAsync();
                    BOMApiResponse apiResponse = JsonConvert.DeserializeObject<BOMApiResponse>(jsonResponse);
                    
                    if (apiResponse.code == 200)
                    {
                        if (apiResponse.data is List<BOMData> bomList)
                        {
                            bomDataList = bomList;
                        }
                        else
                        {
                            // 如果不是正确的类型，尝试转换
                            bomDataList = JsonConvert.DeserializeObject<List<BOMData>>(JsonConvert.SerializeObject(apiResponse.data));
                        }
                        
                        // 按产品分组并填充下拉框
                        PopulateProductComboBox();
                        
                        Console.WriteLine("BOM数据加载完成");
                    }
                }
            }
            catch (Exception ex)
            {
               
            }
        }
        
        
     
      
        
        /// <summary>
        /// 加载产品数据
        /// </summary>
        private async void LoadProductData()
        {
           
                // 显示加载状态
                dgvProducts.DataSource = null;
                
                // 调用产品 API
                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    
                   
                    var response = await client.GetAsync("http://localhost:5281/api/Plan/GetProduct");
                    response.EnsureSuccessStatusCode();
                    
                    string jsonResponse = await response.Content.ReadAsStringAsync();
                    
                    var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(jsonResponse);
                    
                    if (apiResponse?.code == 200 && apiResponse.data != null)
                    {
                      
                        // 转换数据 - 与SelectProduct.cs保持一致
                        productList = apiResponse.data.Select(p => new Product
                        {
                            ProductId = p.productId,
                            ProductCode = p.productCode,
                            ProductName = p.productName,
                            Specification = p.specification,
                            UnitName = p.unitName
                        }).ToList();
                        
                        // 设置产品数据到DataGridView
                        dgvProducts.DataSource = productList;
                        
                        // 调整列宽
                        dgvProducts.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                     
                    }
                 
                }
            
        }
        
        /// <summary>
        /// 加载物料明细数据
        /// </summary>
        private async void LoadMaterialData()
        {
            try
            {
                Console.WriteLine("=== 开始加载物料明细数据 ===");
                
                // 检查dgvMaterial控件是否存在
                if (dgvMaterial == null)
                {
                    MessageBox.Show("物料明细 DataGridView控件未找到", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                
                // 显示加载状态
                dgvMaterial.DataSource = null;
                
                // 调用物料明细 API
                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    
                    Console.WriteLine("正在调用物料明细API...");
                    var response = await client.GetAsync("http://localhost:5128/api/Material/GetMaterialInfo");
                    response.EnsureSuccessStatusCode();
                    
                    string jsonResponse = await response.Content.ReadAsStringAsync();
                    Console.WriteLine($"物料明细API响应: {jsonResponse}");
                    
                    MaterialApiResponse apiResponse = JsonConvert.DeserializeObject<MaterialApiResponse>(jsonResponse);
                    
                    if (apiResponse?.code == 200 && apiResponse.data != null)
                    {
                        Console.WriteLine($"物料明细API调用成功，数据条数: {apiResponse.data.Count}");
                        
                        // 设置物料明细数据到DataGridView
                        materialList = apiResponse.data;
                        dgvMaterial.DataSource = materialList;
                        
                        // 调整列宽
                        dgvMaterial.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                        
                        Console.WriteLine($"成功加载 {materialList.Count} 个物料明细数据");
                        Console.WriteLine("=== 物料明细数据加载完成 ===");
                    }
                    else
                    {
                        Console.WriteLine($"物料明细API调用失败: code={apiResponse?.code}, message={apiResponse?.message}");
                        MessageBox.Show($"物料明细数据加载失败: {apiResponse?.message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"加载物料明细数据时出错: {ex.Message}");
                MessageBox.Show($"加载物料明细数据时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 填充产品下拉框
        /// </summary>
        private void PopulateProductComboBox()
        {
            try
            {
                if (cboProduct == null) return;
                
                cboProduct.Items.Clear();
                
                // 按产品名称分组
                var productGroups = bomDataList.GroupBy(x => x.productName).ToList();
                
                foreach (var group in productGroups)
                {
                    cboProduct.Items.Add(group.Key);
                }
                
                // 如果有数据，默认选择第一项
                if (cboProduct.Items.Count > 0)
                {
                    cboProduct.SelectedIndex = 0;
                }
                
                Console.WriteLine($"已填充 {cboProduct.Items.Count} 个产品到下拉框");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"填充产品下拉框时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 产品下拉框选择改变事件
        /// </summary>
        private void CboProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cboProduct.SelectedItem == null) return;
                
                string selectedProduct = cboProduct.SelectedItem.ToString();
                DisplayMaterialsForProduct(selectedProduct);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"显示产品物料时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 显示指定产品的物料组成
        /// </summary>
        /// <param name="productName">产品名称</param>
        private void DisplayMaterialsForProduct(string productName)
        {
            try
            {
                if (pnlMaterials == null) return;
                
                // 清除现有内容
                pnlMaterials.Controls.Clear();
                
                // 获取该产品的物料列表
                var materials = bomDataList.Where(x => x.productName == productName).ToList();
                
                if (materials.Count == 0)
                {
                    var lblNoData = new Label();
                    lblNoData.Text = "该产品暂无物料数据";
                    lblNoData.Location = new Point(20, 20);
                    lblNoData.Size = new Size(200, 25);
                    lblNoData.Font = new Font("微软雅黑", 9F);
                    lblNoData.ForeColor = Color.Gray;
                    pnlMaterials.Controls.Add(lblNoData);
                    return;
                }
                
                int yOffset = 20;
                int itemHeight = 80;
                
                foreach (var material in materials)
                {
                    // 创建物料卡片面板
                    var materialCard = new Panel();
                    materialCard.Location = new Point(20, yOffset);
                    materialCard.Size = new Size(pnlMaterials.Width - 60, itemHeight);
                    materialCard.BorderStyle = BorderStyle.FixedSingle;
                    materialCard.BackColor = Color.FromArgb(248, 249, 250);
                    
                    // 物料名称标签
                    var lblMaterialName = new Label();
                    lblMaterialName.Text = $"物料名称：{material.materialName}";
                    lblMaterialName.Location = new Point(10, 10);
                    lblMaterialName.Size = new Size(300, 20);
                    lblMaterialName.Font = new Font("微软雅黑", 9F, FontStyle.Bold);
                    lblMaterialName.ForeColor = Color.FromArgb(52, 73, 94);
                    materialCard.Controls.Add(lblMaterialName);
                    
                    // 物料编号标签
                    var lblMaterialCode = new Label();
                    lblMaterialCode.Text = $"物料编号：{material.materialCode}";
                    lblMaterialCode.Location = new Point(10, 35);
                    lblMaterialCode.Size = new Size(200, 20);
                    lblMaterialCode.Font = new Font("微软雅黑", 8F);
                    lblMaterialCode.ForeColor = Color.FromArgb(127, 140, 141);
                    materialCard.Controls.Add(lblMaterialCode);
                    
                    // BOM编号标签
                    var lblBOMCode = new Label();
                    lblBOMCode.Text = $"BOM编号：{material.bomCode}";
                    lblBOMCode.Location = new Point(220, 35);
                    lblBOMCode.Size = new Size(150, 20);
                    lblBOMCode.Font = new Font("微软雅黑", 8F);
                    lblBOMCode.ForeColor = Color.FromArgb(127, 140, 141);
                    materialCard.Controls.Add(lblBOMCode);
                    
                    // 规格型号标签
                    var lblSpecification = new Label();
                    lblSpecification.Text = $"规格型号：{material.specification}";
                    lblSpecification.Location = new Point(10, 55);
                    lblSpecification.Size = new Size(200, 20);
                    lblSpecification.Font = new Font("微软雅黑", 8F);
                    lblSpecification.ForeColor = Color.FromArgb(127, 140, 141);
                    materialCard.Controls.Add(lblSpecification);
                    
                    // 单位标签
                    var lblUnit = new Label();
                    lblUnit.Text = $"单位：{material.unitName}";
                    lblUnit.Location = new Point(220, 55);
                    lblUnit.Size = new Size(100, 20);
                    lblUnit.Font = new Font("微软雅黑", 8F);
                    lblUnit.ForeColor = Color.FromArgb(127, 140, 141);
                    materialCard.Controls.Add(lblUnit);
                    
                    // 将物料卡片添加到面板
                    pnlMaterials.Controls.Add(materialCard);
                    
                    yOffset += itemHeight + 10;
                }
                
                Console.WriteLine($"已显示产品 '{productName}' 的 {materials.Count} 个物料");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"显示物料信息时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvMaterial_Click(object sender, EventArgs e)
        {

        }
    }
}
